package by.averianov.discounta.services.impl;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.stereotype.Service;

import by.averianov.discounta.datamodel.UserAccount;
import by.averianov.discounta.datamodel.UserProfile;
import by.averianov.discounta.services.EmailSender;
import by.averianov.discounta.services.UserService;

@Service
public class EmailSenderImpl implements EmailSender{	

	private String dns;
	private String port;
	private String title;
	private String tamplate1;
	private String tamplate2;
	private String tamplate3;
	private String tamplate4;
	private HtmlEmail email;
	//private Email email;
	private UserAccount userAccount;
	private UserProfile userProfile;	
	
	@Inject
	private UserService userService;
	
	public void sendEmailConfirm(UserProfile Profile, List<String> emailParameters) throws EmailException {
		
		this.dns = emailParameters.get(0);
		this.port = emailParameters.get(1);
		this.title = emailParameters.get(2);
		this.tamplate1 = emailParameters.get(3);
		this.tamplate2 = emailParameters.get(4);
		this.tamplate3 = emailParameters.get(5);
		this.tamplate4 = emailParameters.get(6);
		this.email = new HtmlEmail();
		//this.email = new SimpleEmail();
		this.userProfile = Profile;
		this.userAccount = userService.getUserAccount(userProfile.getId());
		// create the email message
				final String AUTHUSER= "testforepam@gmail.com";
				final String AUTHPWD = "Ghblevfk01";
				
				
				email.setHostName("smtp.gmail.com");
				email.setSmtpPort(465);
				email.setAuthenticator(new DefaultAuthenticator(AUTHUSER, AUTHPWD));
				email.setSSLOnConnect(true);
				email.setFrom("testforepam@gmail.com");
				email.addTo(userAccount.getUserEmail(), userProfile.getUserFirstName() + " " + userProfile.getUserLastName());

				email.setSubject("Confirmprofile creation");
				
				// embed the image and get the content id
				URL urlDefault = null;
				URL urlTitleImage = null;
				
				try {
					urlDefault = new URL("http", dns, Integer.parseInt(port), "/");
					urlTitleImage = new URL(urlDefault, "images/logo.png");
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
				
				String cid = email.embed(urlTitleImage, title);
				String activateCode = userAccount.getActivateCode();
				
				// Example: "/page/f1/f2"
				String activateLink = "\"" + urlDefault + "login/" + userProfile.getId().toString() + "/" + userAccount.getActivateCode() + "\"";
				
				String htmlEmailTemplate = "<html><h2>" + title + "</h2>"
						+ " - <img src=\"cid:" + cid + "\"><br>"
						+ tamplate2 + "<br>"						
						+ tamplate1 + " <h2>" + activateCode + "</h2><br>"
						+ tamplate3 + " <h2>" + "<a href=" + activateLink + ">activateLink</a></h2><br>"
						+ tamplate4 + " </html>";	
				
				// set the html message
				email.setHtmlMsg(htmlEmailTemplate);
				
				// set the alternative message
				email.setTextMsg("Your email client does not support HTML messages");
				
				// send the email
				email.send();
	}
}
